package zhuyun.code.datamanager.view

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import androidx.lifecycle.Observer
import com.google.android.material.snackbar.Snackbar
import zhuyun.code.datamanager.R
import zhuyun.code.datamanager.databinding.ActivityAddAccountBinding
import zhuyun.code.datamanager.model.Account
import zhuyun.code.datamanager.viewmodel.AccountViewModel
import zhuyun.code.datamanager.viewmodel.ViewModelManager

class AddAccountActivity : AppCompatActivity() {
    var binding: ActivityAddAccountBinding? = null
    var mAccountModelView :AccountViewModel? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this, R.layout.activity_add_account)
        mAccountModelView = ViewModelManager<AccountViewModel>().getViewModel(this,AccountViewModel::class.java)
        mAccountModelView!!.addSuccess.observe(this, Observer<Boolean> { t ->
            when(t){
                true -> {
                    binding?.constraintlayout?.let { Snackbar.make(it, "新增成功", Snackbar.LENGTH_SHORT).show() }
                    finish()
                }
                false -> {
                    binding?.constraintlayout?.let { Snackbar.make(it, "新增失败", Snackbar.LENGTH_SHORT).show() }
                }

            }
        })
        val account: Account= Account()
        binding?.account = account
        binding?.addBt?.setOnClickListener {
            Log.d("AddAccountActivity","${binding!!.username}")
            mAccountModelView!!.addAccount(binding!!.account)
        }

    }
}

private operator fun Boolean.invoke(function: () -> Unit) {

}
